<?php

class TShirtRequest extends CActiveRecord
{
	/**
	 * The followings are the available columns in table 'TShirtRequest':
	 * @var integer $id
	 * @var string $name
	 * @var integer $age
	 * @var string $profession
	 * @var string $phone
	 * @var string $email
	 * @var string $zone
	 * @var string $city
	 * @var integer $state
	 * @var string $country
	 * @var string $address
	 * @var integer $sent
	 * @var integer $found
	 * @var string $donation
	 * @var integer $authorId
	 * @var string $comment
	 * @var integer $createTime
	 * @var integer $updateTime
	 */

	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'TShirtRequest';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		return array(
			array('name','length','max'=>50),
			array('profession','length','max'=>100),
			array('phone','length','max'=>50),
			array('email','length','max'=>255),
			array('zone','length','max'=>100),
			array('city','length','max'=>50),
			array('country','length','max'=>50),
			array('address','length','max'=>255),
			array('donation','length','max'=>20),
			array('comment','length','max'=>500),
			array('name, authorId, createTime, updateTime', 'required'),
			array('age, state, sent, found, authorId, createTime, updateTime', 'numerical', 'integerOnly'=>true),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'Id',
			'name' => 'Nombre',
			'age' => 'Edad',
			'profession' => 'Oficios',
			'phone' => 'Teléfono',
			'email' => 'E-mail',
			'zone' => 'Zona',
			'city' => 'Población',
			'state' => 'Provincia',
			'country' => 'Pais',
			'address' => 'Dirección',
			'sent' => 'Camiseta entregada',
			'found' => 'Ha encontrado trabajo',
			'donation' => 'Donación',
			'authorId' => 'Añadido por',
			'comment' => 'Comentarios',
			'createTime' => 'Fecha creación',
			'updateTime' => 'Última modificación',
		);
	}
	
	/**
	 * Prepares attributes before performing validation.
	 */
	protected function beforeValidate($on)
	{
		if($this->isNewRecord)
		{
			$this->createTime=$this->updateTime=time();
			$this->authorId=Yii::app()->user->id;
      $this->secure_key=md5($this->name.$this->profession.$this->email.time());
		}
		else
			$this->updateTime=time();
		return true;
	}
	
	public function getHasTShirt() {
		return ($this->sent == 1) ? 'Sí' : 'No';
	}
	public function getFoundJob() {
		return ($this->found == 1) ? 'Sí' : 'No';
	}
	public function getAuthorName() {
		$author = User::model()->findByPk($this->authorId);
		return $author->realname;
	}
  public function getYesNoOptions()
	{
		return array(
			0=>'No',
			1=>'Sí'
		);
	}
  public function getStateOptions()
	{
    $states = State::model()->findAll();
    $result = array();
    foreach ($states as $state) {
      $result[$state->id] = CHtml::encode($state->name);
    }
    return $result;
  }
  public function getStateName() {
    if ($this->state) {
      $state = State::model()->findByPk($this->state);
      return $state->name;
    }
    return '';
  }

  /**
   * Convert string to slug
   * @param string $title
   * @return mixed $slug
   */
  static public function makeSlug($title){
      $replace_space = '-';
      $clean = iconv('UTF-8', 'ASCII//TRANSLIT', $title);
      $clean = preg_replace("/[^a-zA-Z0-9\/_| -]/", '', $clean);
      $clean = strtolower(trim($clean));
      $clean = preg_replace("/[\/_| -]+/", $replace_space, $clean);

      return $clean;
  }

  public function getProfessionToSlug() {
    if ($this->profession) {
      $toSlug = self::makeSlug($this->profession);
      return $toSlug;
    }
    return '';
  }

  public function getZoneToSlug() {
    if ($this->zone) {
      $toSlug = self::makeSlug($this->zone);
      return $toSlug;
    }
    return '';
  }
  
}